//
//  PhotoResultViewController.m
//  FaceRecognition
//
//  Created by zz on 16/12/5.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "PhotoResultViewController.h"

@interface PhotoResultViewController ()

@property (nonatomic,strong) UIImageView *imageView;

@end

@implementation PhotoResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    if (_type == 2) {
        
        _imageView.frame = CGRectMake(0,0,_oriImage.size.width / 2, _oriImage.size.height / 2);
    }

    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.center = self.view.center;
    _imageView.userInteractionEnabled = YES;
    _imageView.image = _oriImage;
//    _imageView.image = UIViewContentModeScaleToFill;
//    _imageView.contentMode = UIViewContentModeScaleAspectFill;
//    _imageView.frame = CGRectMake(0, 0, _selectImage.size.width, _selectImage.size.height);
    
    [self.view addSubview:_imageView];
    
    [self testFace];
    
    
}

/**识别人脸*/
- (void)testFace{

    CIImage *cImage = [CIImage imageWithCGImage:_selectImage.CGImage];
    NSDictionary *options = [NSDictionary dictionaryWithObject:CIDetectorAccuracyLow forKey:CIDetectorAccuracy];
    
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:options];
    
    //得到面部数据
    NSArray *features = [detector featuresInImage:cImage];
    
    if (features.count) {
        
        //设置底部按钮
        [self setupBottomButton];
        
        for (CIFaceFeature *f in features) {
            
            CGRect aRect = f.bounds;
            
            CGFloat w = aRect.size.width;
            w = w * _scale;
            
            CGFloat x,y,h;
            
            x =  _scale * (aRect.origin.x);
            x += 20;
            y = (self.selectImage.size.height -  aRect.origin.y - aRect.size.height) * _scale;
            y -= 10;
            h = aRect.size.height * _scale;
            h += 20;
            

            
            UIImageView *bgView = [[UIImageView alloc] init];
            
            if (_type == 1) {
                
                bgView.frame = CGRectMake(x, y, w, h);
            }else if (_type == 2){
                
                bgView.frame = CGRectMake(aRect.origin.x / 2, self.imageView.frame.size.height - aRect.origin.y / 2 - aRect.size.width / 2, aRect.size.width / 2, aRect.size.height / 2);
            }
            
            bgView.image = [UIImage imageNamed:@"frame_"];
            
            [_imageView addSubview:bgView];
            
            NSLog(@"%.f, %.f, %.f, %.f", aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);
        }
        
    }else{  //没有识别到人脸
        
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 254, 135)];
        view.backgroundColor = [UIColor whiteColor];
        view.alpha = 0.8;
        view.userInteractionEnabled = YES;
        view.center = self.imageView.center;
        view.layer.cornerRadius = 10;
        [self.view addSubview:view];
        
        //未检测到人脸
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 37, view.frame.size.width, 24)];
        label.font = [UIFont systemFontOfSize:24];
        label.text = @"没有检测到人脸";
        label.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        label.textAlignment = NSTextAlignmentCenter;
        [view addSubview:label];
        
        //从新拍摄
        UIButton *retakeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        retakeBtn.frame = CGRectMake(62, CGRectGetMaxY(label.frame) + 28, 130, 30);
        retakeBtn.layer.cornerRadius = 5;
        retakeBtn.layer.borderWidth = 2;
        retakeBtn.layer.borderColor = [UIColor colorWithHexString:@"#ffb931"].CGColor;
        NSString *title = _type == 1 ? @"重新拍摄" : @"重新选择";
        [retakeBtn setTitle:title forState:UIControlStateNormal];
        [retakeBtn setTitleColor:[UIColor colorWithHexString:@"#ffb931"] forState:UIControlStateNormal];
        [retakeBtn addTarget:self action:@selector(reTakePhotoAction) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:retakeBtn];
        
    }
    
}

- (void)setupBottomButton{
    
    CGFloat x,y,w;
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    w = (VIEW_WIDTH - 48 - 25) / 2;
    
    [confirmBtn setFrame:CGRectMake(24, VIEW_HEIGHT - 75, w, 44)];
    [confirmBtn setTitle:@"确认使用" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [confirmBtn setBackgroundColor:[UIColor colorWithHexString:@"#39ADFF"]];
    [confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    confirmBtn.layer.cornerRadius = 5;
    [self.view addSubview:confirmBtn];
    
    UIButton *reTakeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [reTakeBtn setFrame:CGRectMake(CGRectGetMaxX(confirmBtn.frame) + 25, VIEW_HEIGHT - 75, w, 44)];
    NSString *title = _type == 1 ? @"重新拍摄" : @"重新选择";
    [reTakeBtn setTitle:title forState:UIControlStateNormal];
    reTakeBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [reTakeBtn setBackgroundColor:[UIColor colorWithHexString:@"#FFB931"]];
    [reTakeBtn addTarget:self action:@selector(reTakePhotoAction) forControlEvents:UIControlEventTouchUpInside];
    reTakeBtn.layer.cornerRadius = 5;
    [self.view addSubview:reTakeBtn];
}


//确定上传
- (void)confirmAction{
    
    RequestParam *param = [RequestParam param];
    param.subject_id = _faceId;
    
    [self showHudInView:self.view hint:@"上传中..."];
    
    [Utils uploadImage:@"/subject/photo" parameters:param.mj_keyValues image:_oriImage success:^(AFHTTPRequestSerializer *option, id responjson) {
        
        NSDictionary *dict = [responjson objectForKey:@"data"];
        
        
        if (dict.allKeys.count > 0) {
            
            NSNumber *photoId = [dict objectForKey:@"id"];
            
            [self hideHud];
            
            
            [self showHint:@"上传成功"];
            
            [self dismissViewControllerAnimated:YES completion:^{
                
                //上一个控制器也dismiss掉
                if (self.flag == 1) {
                    
                    if (_block2) {
                        
                        _block2(_oriImage,_photoCell,photoId);
                    }
                }else
                {
                    if (_block) {
                        
                        _block(_oriImage,_photoV,photoId);
                    }
                }
               
            }];
        }else{
            
            NSString *desc = [responjson objectForKey:@"desc"];
            
            [self showHint:desc];
            
            [self dismissViewControllerAnimated:YES completion:^{
               
                if (self.flag == 1) {
                    
                    if (_block2) {
                        
                        _block2(nil,nil,@-1);
                    }
                }else
                {
                    if (_block) {
                        
                        _block(nil,nil,@-1);
                    }
                }
            }];
        }
        
        
    } failure:^(AFHTTPRequestSerializer *option, NSError *error) {
        
        [self hideHud];
        [self showHint:error.localizedDescription];
    }];
    
    
}

//重新拍摄
- (void)reTakePhotoAction{
    
    [self dismissViewControllerAnimated:YES completion:^{
        

        if (self.flag == 1) {
            
            if (_block2) {
                
                _block2(nil,nil,nil);
            }
        }else
        {
            if (_block) {
                
                _block(nil,nil,nil);
            }
        }
    }];
}

@end
